﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorldEvolution
{    
    public class Entity : Object
    {
        public PlaceComponent Place { get { return mPlace; } }

        public Entity()
        {
            mPlace = AddComponet<PlaceComponent>();
        }
        
        public static T Born<T>(Cell cell)
            where T:Entity,new()
        {
            T entity = new T();
            EventManager.Instance.Trigger<EventEntityBorn>(EventEntityBorn.Get().reset(cell), entity.ID);
            return entity;
        }
        
        public T GetComponet<T>()
            where T : IComponent
        {
            var c = mComponentDict[typeof(T)];
            if (c == null)
                return null;
            return c as T;
        }

        public T AddComponet<T>() where T : IComponent,new()
        {
            T ret = new T();
            mComponentDict[typeof(T)] = ret;
            ret.Initalize(this);
            return ret;
        }

        private PlaceComponent mPlace;
        private Dictionary<Type, IComponent> mComponentDict = new Dictionary<Type, IComponent>();

    }
}
